from typing import List

from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait

from src.PO.log_util import logger


class Base:
    def __init__(self,driver:WebDriver = None):
        if not driver:
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
            self.driver.implicitly_wait(3)
        else:
            self.driver=driver

    def find(self, by, value=None) -> WebElement:
        if isinstance(by, tuple):
            logger.debug(f"使用 {by[0]} 的定位方式,进行元素 {by[1]} 的定位")
            ele = self.driver.find_element(*by)
        else:
            logger.debug(f"使用 {by} 的定位方式,进行元素 {value} 的定位")
            ele = self.driver.find_element(by, value)
        return ele

    def finds(self, by, value=None) -> List[WebElement]:
        if isinstance(by, tuple):
            logger.debug(f"使用 {by[0]} 的定位方式,进行元素 {by[1]} 的定位")
            eles = self.driver.find_elements(*by)
        else:
            logger.debug(f"使用 {by} 的定位方式,进行元素 {value} 的定位")
            eles = self.driver.find_elements(by, value)
        return eles

    def find_and_click(self, by, value=None):
        """
        查找并点击
        :param by:
        :param value:
        :return:
        """
        ele = self.find(by=by, value=value)
        ele.click()

    def find_and_send(self, by, value=None, text=None):
        """
        查找并输入
        :param by:
        :param value:
        :param text:
        :return:
        """

        ele = self.find(by=by, value=value)
        logger.debug(f"进行信息{text}的输入操作")
        ele.send_keys(text)
